﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WssClient.Item.ZhongFu
{
    /// <summary>
    /// 跑马重产品
    /// </summary>
    public class Product
    {
        /// <summary>
        /// 序号
        /// </summary>
        public int idx { get; set; }
        /// <summary>
        /// 货品名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 件数
        /// </summary>
        public int packages { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public double number { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        public string units { get; set; }
        /// <summary>
        /// 单价
        /// </summary>
        public double price { get; set; }
        /// <summary>
        /// 金额
        /// </summary>
        public double amount { get; set; }
        /// <summary>
        /// 跑马重数量明细
        /// </summary>
        public List<string> monkeyHeavyNumberDetails { get; set; } = new List<string>();

        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="data">未转化产品数据对象</param>
        /// <param name="isGeneral">是否常规产品</param>
        /// <returns></returns>
        public static Product Analysis(dynamic data, bool isGeneral)
        {
            Product product = new Product();
            try
            {
                if (isGeneral)
                {
                    product.name = data.general_name;

                    string packagesStr = data.general_packages;
                    product.packages = !string.IsNullOrEmpty(packagesStr) ? int.Parse(packagesStr) : 0;

                    string numberStr = data.general_number;
                    product.number = !string.IsNullOrEmpty(numberStr) ? double.Parse(numberStr) : 0;

                    product.units = data.general_units;

                    string priceStr = data.general_price;
                    product.price = !string.IsNullOrEmpty(priceStr) ? double.Parse(priceStr) : 0;

                    string amountTotalStr = data.general_amount;
                    product.amount = !string.IsNullOrEmpty(amountTotalStr) ? double.Parse(amountTotalStr) : 0;
                }
                else
                {
                    product.name = data.name;

                    string packagesStr = data.packages;
                    product.packages = !string.IsNullOrEmpty(packagesStr) ? int.Parse(packagesStr) : 0;

                    string numberStr = data.number;
                    product.number = !string.IsNullOrEmpty(numberStr) ? double.Parse(numberStr) : 0;

                    product.units = data.units;

                    string priceStr = data.price;
                    product.price = !string.IsNullOrEmpty(priceStr) ? double.Parse(priceStr) : 0;

                    string amountTotalStr = data.amount;
                    product.amount = !string.IsNullOrEmpty(amountTotalStr) ? double.Parse(amountTotalStr) : 0;

                    string details = data.monkeyHeavyNumberDetails;

                    //每行10个数值，超过自动换行
                    int _total = 8;
                    string[] detailArr = details.Split(',');
                    int _length = detailArr.Length;
                    int _idx = 0;
                    int _endx = _length > _total ? _total-1 : _length - 1;

                    while (_endx > -1 && _idx < _length)
                    {
                        string _val = "";
                        do
                        {
                            _val += $"{detailArr[_idx]}  ";
                        } while (_idx++ < _endx);
                        product.monkeyHeavyNumberDetails.Add(_val);
                        _endx = _length > _idx + _total ? _idx + _total - 1 : _length - 1;
                    }

                    //details = string.IsNullOrEmpty(details) ? "" : details.Replace(",", "  ");
                    //int start = 0;
                    //int width = 55;
                    //int length = details.Length;

                    //do
                    //{
                    //    int end = width;
                    //    end = end > length ? length : end;

                    //    string _details = details.Substring(start, end);
                    //    details = details.Substring(end);
                    //    length = details.Length;

                    //    product.monkeyHeavyNumberDetails.Add(_details);
                    //} while (length > 0);
                }
            }
            catch (Exception e)
            {
                
            }

            return product;
        }
    }
}
